/**
 * 静态文件处理
 * */

package com.framerock.server;



import com.framerock.baselib.Config;
import com.framerock.baselib.Rock;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class HttpStatic {

    /**
     * 客户端信息
     * */
    private HttpClient httpClient;

    /**
     * 文件的路径
     * */
    private String filepath;

    /**
     * 文件扩展名
     * */
    private String fileext;

    /**
     * 状态值
     * */
    private int code = 404;

    /**
     * 文件大小
     * */
    private long filesize = 0;

    /**
     * 静态文件处理读取
     * */
    public HttpStatic(HttpClient httpclient)
    {
        httpClient      = httpclient;
    }

    /**
     * 读取内容
     * */
    public void writeBody(String path)
    {
        filepath        = path;
        String type     = getContentType(path);
        String cont     = getFilecont();
        boolean bool    = isReadBuffer(fileext);
        httpClient.setHttpStatus(this.code);
        httpClient.setBaseHeader(type);
        if(bool && filesize>0)httpClient.setContentLength(filesize);
        if(this.code == 404){
            cont = "FILE ("+path.substring(1)+") 404 NOT FOUND";
            bool = false;
        }
        httpClient.setHttpBody(cont);
        httpClient.writeResponse();
        if(bool){
            readBuffer();
        }
    }

    /**
     * 获取文件的表头
     * */
    private String getContentType(String path)
    {
        String arr[]    = path.split("\\.");
        fileext = arr[0];
        if(arr.length > 1)fileext = arr[arr.length-1];
        fileext         = fileext.toLowerCase();
        String type     = "text/html;charset=utf-8";
        if(fileext.equals("css"))type       = "text/css";
        if(fileext.equals("js"))type        = "application/javascript";
        if(fileext.equals("png"))type       = "image/png";
        if(fileext.equals("jpg"))type       = "image/jpeg";
        if(fileext.equals("gif"))type       = "image/gif";
        if(fileext.equals("woff"))type      = "application/x-font-woff";
        if(fileext.equals("ttf"))type       = "application/x-font-woff";
        if(fileext.equals("otf"))type       = "application/x-font-woff";
        if(fileext.equals("svg"))type       = "image/svg+xml";
        if(fileext.equals("ico"))type       = "image/x-icon";
        if(fileext.equals("mp3"))type       = "audio/mpeg";
        return type;
    }

    /**
     * 获取文件内容
     * */
    public String getFilecont()
    {
        String dir      = Config.getConfig("web.path");
        if(Rock.isEmpt(dir))return "";
        String path     = ""+dir+""+filepath+"";
        String cont     = ""; int xu = 0;
        File file       = new File(path);
        if(file.exists()){
            this.code   = 200;
            filesize    = file.length();
            if(isReadBuffer(fileext)) return "";
            try (Scanner scanner = new Scanner(file)){
                while(scanner.hasNextLine()){
                    if(xu>0)cont += "\n" ;
                    cont += scanner.nextLine();
                    xu++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                this.code = 500;
            }
        }
        return cont;
    }

    /**
     * 判断是否需要读取对象
     * */
    private Boolean isReadBuffer(String ext)
    {
        String strs = ",jpg,png,gif,woff,ttf,otf,ico,mp3,";
        return Rock.contain(strs, ","+ext+",");
    }

    /**
     * 读取对象的
     * */
    private void readBuffer()
    {
        String dir      = Config.getConfig("web.path");
        String path     = ""+dir+""+filepath+"";
        try {
            FileInputStream file = new FileInputStream(new File(path));
            byte[] buffer = new byte[1024*10]; int len;
            while ((len = file.read(buffer)) != -1) {
                httpClient.writeResponse(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}